//
//  ZFCustomControlView.m
//  ZFPlayer_Example
//
//  Created by 紫枫 on 2019/6/5.
//  Copyright © 2019 紫枫. All rights reserved.
//
#define defaultBtn_w 70
#define defaultBtn_H (40 + 22)
#define defaultBtn_margin 40
#define contentBtn_tag 10000

#import "LandScapeShareCoverView.h"
#import "UIButton+offsetIMG.h"
@interface LandScapeShareCoverView ()
/// 加入授课包
@property (nonatomic, strong) NSMutableArray *contentArr;
@end

@implementation LandScapeShareCoverView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        [self.contentArr setArray:[self getContentArr]];
        [self addAllSubViews];
        /// statusBarFrame changed
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layoutControllerViews) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //    CGFloat min_margin = 9;
    CGFloat min_view_w = self.bounds.size.width;
    CGFloat min_view_h = self.bounds.size.height;
    
    self.BGView.frame = CGRectMake(0, 0, min_view_w, min_view_h);

    [self  layoutContent:self.contentArr];
    CGFloat content_w = (defaultBtn_w + defaultBtn_margin)*(self.contentArr.count);
    self.contentView.frame = CGRectMake((min_view_w - content_w)/2, 0, content_w, min_view_h);
//    self.contentView.frame = CGRectMake(0, 0, 600, min_view_h);
}

- (void)layoutControllerViews {
    [self layoutIfNeeded];
    [self setNeedsLayout];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}

/// 添加所有子控件
- (void)addAllSubViews {
    [self addSubview:self.BGView];
    [self addSubview:self.contentView];

    [self addBtnToContent:self.contentArr];

}
-(NSArray *)getContentArr{
    NSMutableArray *contentArr = [NSMutableArray arrayWithCapacity:4];
    id fileContent = [UXUtilities getFile:@"shareList"];
    if ([fileContent isKindOfClass:[NSDictionary class]]) {
        id shareList = [fileContent objectForKey:@"shareList"];
        if ([shareList isKindOfClass:[NSArray class]]) {
            [contentArr setArray:shareList];
        }
    }
    return contentArr;
}
- (UIView *)BGView {
    if (!_BGView) {
        _BGView = [[UIView alloc] init];
        _BGView.backgroundColor = [UIColor blackColor];
        _BGView.alpha = 0.65;
    }
    return _BGView;
}
- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}
- (NSMutableArray *)contentArr {
    if (!_contentArr) {
        _contentArr = [[NSMutableArray alloc] init];
    }
    return _contentArr;
}
-(void)addBtnToContent:(NSArray *)typeArr{
    for (int i = 0; i < typeArr.count;  i++) {
        NSDictionary *typeDic = [typeArr objectAtIndex: i];
        NSString *type = [typeDic objectForKey:@"type"];
        NSString *icon = [typeDic objectForKey:@"icon"];
        NSString *title = [typeDic objectForKey:@"title"];
        ShareButton *typeBtn = [self creatTypeBtn:icon title:title type:type];
        typeBtn.tag =  contentBtn_tag + i;
        CGFloat min_view_h = self.bounds.size.height;
        typeBtn.frame = CGRectMake(i * (defaultBtn_w + defaultBtn_margin), (min_view_h - defaultBtn_H)/2, defaultBtn_w, defaultBtn_H);
        [self.contentView addSubview:typeBtn];
    }
}

-(void)layoutContent:(NSArray *)typeArr{
    for (int i = 0; i < typeArr.count;  i++) {
        CGFloat min_view_h = self.bounds.size.height;
        ShareButton *typeBtn = [self.contentView viewWithTag:(contentBtn_tag + i)];
        typeBtn.frame = CGRectMake(i * (defaultBtn_w + defaultBtn_margin), (min_view_h - defaultBtn_H)/2, defaultBtn_w, defaultBtn_H);
    }
}

- (ShareButton *)creatTypeBtn:(NSString *)icon title:(NSString *)title type:(NSString *)type{

    ShareButton *typeBtn = [ShareButton buttonWithType:UIButtonTypeSystem];
    typeBtn.type = type;
    typeBtn.icon = icon;
    typeBtn.title = title;
//    typeBtn.backgroundColor = [UIColor whiteColor];
    [typeBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIImageView * upImg = [[UIImageView  alloc] initWithFrame:CGRectMake((defaultBtn_w - 40)/2, 0, 40, 40)];
    upImg.image = UXUtilities_Image(icon);
    [typeBtn addSubview: upImg];

    UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, defaultBtn_w, 22)];
    content.text = title;
    content.font = [UIFont systemFontOfSize:10];
    content.textColor = [UIColor whiteColor];
    content.textAlignment = NSTextAlignmentCenter;
    [typeBtn addSubview: content];

    return typeBtn;
}

-(void)addBtnClick:(ShareButton  *)sender {
    if (self.shareBlock != nil) {
        self.shareBlock(sender.title);
    }
}

@end
